// mall/pages/goodslist.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_info:{},
    loading: 0,
    page: 1,
    status: 0,
    lastpage: 0,
    listData: [],
    attr_id:26,
    order:"MallGoods.order_by desc",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.shop_info = app.getShopInfo();
    if (options.attr_id){
      this.data.attr_id = options.attr_id;
    }
    this.loadList();
    this.loadTagInfo();
  },

  //加载购物车
  loadTagInfo(reload = 0) {
    var that = this;
    if (reload == 1) {
      this.setData({
        loading: 0,
        data: {},
      });
    }
    this.data.loading = 1;
    app.api("mall/tag/find", {
      id: this.data.attr_id
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      wx.setNavigationBarTitle({
        title: res.data.name,
      })
      
    })
  },

  setOrder: function (e) {
    var order = e.currentTarget.dataset.order;
    if (order=="price"){
      order = this.data.order == "MallGoods.price" ? 'MallGoods.price desc' :"MallGoods.price";
    }
    if (this.data.order == order) {
      return;
    }

    this.setData({ order: order });
    this.loadList(1);
    //console.log(e);
  },

  loadList: function (reload) {
    var that = this;

    if (reload == 1) {
      this.setData({
        loading: 0,
        page: 1,
        lastpage: 0,
        listData: [],
      });
    }

    var page = this.data.page;
    if (this.data.loading == 1 || this.data.lastpage == 1) {
      //console.log(this.data);
      return;
    }
    this.data.loading = 1;
    app.api("mall/goods/get", {
      page: page,
      limit: 30,
      attr_id: this.data.attr_id,
      order: this.data.order,
      fields: 'id,goods_name,total_stock,origin_price,price,sub_title,order_count,shop_id,default_img'

    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length == 0) {
        that.data.lastpage = 1;
      }
      that.data.page++;
      that.setData({
        listData: that.data.listData.concat(res.data.list),
      });
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadList(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})